<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        //原来方式
        (function () {
            function Test(name) {
                this.name = name
            }
            Test.prototype.A = function () {
                console.log('A')
            }
            Test.prototype.B = function () {
                console.log('B')
            }
            let Test1 = new Test('测试')
            Test1.A()
            Test1.B()
        })()
    </script>
    <script>
            //手写new实现原理
            (function () {
                function Test(name) {
                    this.name = name
                }
                function _new(Func, ...args) {
                    let obj = {} //创建实例对象，属于这个函数的类
                    let result = Func.call(obj, ...args)//将this 指向实例
                    //实例或者自己返回的对象
                    if ((result !== null && typeof result === 'object') || typeof result === 'function') {
                        return result
                    } else {
                        return obj
                    }
                }
                let ReturnData = _new(Test, 'abc')
                console.log(ReturnData) //{name:'abc'}
            })()
    </script>

</body>

</html>